package demo05;

/**
 * @author: WangPeng
 * @createTime: 2022/11/01 9:46
 * @description:
 * @version: v1.0
 */
public class TestYield {
    public static void main(String[] args) {
        YieldDemo yieldDemo1 = new YieldDemo("线程A");
        YieldDemo yieldDemo2 = new YieldDemo("线程B");
        yieldDemo1.start();
        yieldDemo2.start();
    }
}
class YieldDemo extends Thread{
    public YieldDemo(){ }
    public YieldDemo(String name){
        super(name);
    }
    @Override
    public void run() {
        for (int i = 0; i < 20; i++) {
            //当前线程放弃时间片，进入就绪状态，参与下次cpu时间片的竞争。 可能出现交替的频率比较高
            Thread.yield();
            System.out.println(Thread.currentThread().getName()+"执行====="+i);
        }
    }
}
